﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;

namespace DAL
{
    public class MemberDAL : SqlDataProvider
    {
        #region[Member_GetById]
        public List<Member> Member_GetById(string Id)
        {
            List<Entity.Member> list = new List<Entity.Member>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_GetById", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Member.MemberIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Member_GetByTop]
        public List<Member> Member_GetByTop(string Top, string Where, string Order)
        {
            List<Entity.Member> list = new List<Entity.Member>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_GetByTop", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Member.MemberIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Member_GetByAll]
        public List<Member> Member_GetByAll()
        {
            List<Entity.Member> list = new List<Entity.Member>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_GetByAll", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Member.MemberIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Member_ChangePass]
        public bool Member_ChangePass(string UserName, string NewPassword)
        {
            try
            {
                ExecuteNonQuery("Update [Member] set [password]='" + NewPassword + "' where Username= '" + UserName + "'");
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
        #region[Member_Insert]
        public bool Member_Insert(Member data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Email", data.Email));
                dbCmd.Parameters.Add(new SqlParameter("@Username", data.Username));
                dbCmd.Parameters.Add(new SqlParameter("@Password", data.Password));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Member");
            return true;
        }
        #endregion
        #region[Member_Update]
        public bool Member_Update(Member data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Email", data.Email));
                dbCmd.Parameters.Add(new SqlParameter("@Username", data.Username));
                dbCmd.Parameters.Add(new SqlParameter("@Password", data.Password));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Member");
            return true;
        }
        #endregion
        #region[Member_Delete]
        public bool Member_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Member_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Member");
            return true;
        }
        #endregion

    }
}
